/**Copyright (C) 2011 Ron Coleman. Contact: ronncoleman@gmail.com

 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 **/

package bj.gui

import scala.swing.event
import event.ButtonClicked
import scala.swing.SimpleGUIApplication
import scala.swing.MainFrame
import scala.swing.Label
import scala.swing.Swing
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.BorderPanel
import scala.swing.BoxPanel
import scala.swing.Orientation
import scala.swing.Table
import scala.swing.ScrollPane
import scala.swing.MenuBar
import scala.swing.Menu
import scala.swing.MenuItem
import scala.swing.Action
import java.awt.Dimension
import javax.swing.ImageIcon
import scala.swing.event.TableRowsSelected
import javax.swing.table.DefaultTableModel
import org.apache.log4j.PropertyConfigurator
import bj.util.Logs
import java.awt.Color
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
import org.apache.log4j.Logger
import org.apache.log4j.Level
import bj.actor.Player
import bj.actor.House
import bj.actor.Dealer
import scala.actors.Actor
import bj.actor.Go
import bj.actor.Stop
import scala.actors.OutputChannel
import scala.swing.Dialog
import bj.util.Config

/**
 * @author Vikram
 * This class starts the House and Display the table with Dealer, Table and Player Info
 **/

object BjStatistics extends SimpleGUIApplication with Logs {
  javax.swing.UIManager.setLookAndFeel(new NimbusLookAndFeel)
  PropertyConfigurator.configure("log4j.properties")

  def top = bjFrame
  
  /** Creating MainFframe for BjServer */
  val bjFrame = new MainFrame {
    title = "Blackjack - House Overiew"
    minimumSize = new Dimension(700, 300)
    resizable = false

    /** Creating a Menubar with File and About BlackJack game */
    menuBar = new MenuBar {
      contents += new Menu("File") {
        contents += new MenuItem(Action("Exit") {
          System.exit(0);
        })
      }
      contents += new Menu("About") {
        contents += new MenuItem(Action("About") {
          Dialog.showMessage(null, Config.About, "About", Dialog.Message.Info, new ImageIcon("images/BlackJackInfo.png"))
        })
      }
    }

    /** Creating Start House Buttons */
    val startHouseButton = new Button { text = "Start House" }
    
    /** Creating Stop House Buttons */
    val stopHouseButton = new Button {
      text = "Stop House"
      enabled = false
    }

    /** Creating Table data to display */
    val model = Array(
      Array("0", "0", "100", "200", "RonBot100", "$10000.00"),
      Array("1", "1", "25", "99", "RonBot25", "$5000.00"),
      Array("2", "2", "5", "24", "RonBot5", "$1000.00"))

      /** Creating Table headers */
    val headers = Array("Table #", "DealerId", "Minimum Bet", "Maximum Bet", "Players", "BankRoll")

    /** Creating Table Model */
    val tableModel = new DefaultTableModel(
      model.asInstanceOf[Array[Array[Object]]], // Data
      headers.asInstanceOf[Array[Object]]) // Column names
    val tablesTable = new Table(model.size, headers.size) {
      model = tableModel
      enabled = false
      visible = false
    }

    /** Creating Top Panel for Image */
    val topPanel = new BorderPanel() {
      add(new Label {
        icon = new ImageIcon("images/blackjack.png")
      }, BorderPanel.Position.North)
      background = new Color(58, 129, 159)
    }
    
    /** Creating MidPanel for the Table */
    val midPanel = new FlowPanel() {
      contents +=
        new ScrollPane {
          viewportView = tablesTable
          preferredSize = new Dimension(650, 80)
        }
      background = new Color(58, 129, 159)
    }

    /** Creating Button Panel for Start and Stop Buttons */
    val buttonPanel = new FlowPanel() {
      contents += startHouseButton
      contents += stopHouseButton
      background = new Color(58, 129, 159)
    }

    contents = new BorderPanel() {
      add(topPanel, BorderPanel.Position.North)
      add(midPanel, BorderPanel.Position.Center)
      add(buttonPanel, BorderPanel.Position.South)
      border = Swing.EmptyBorder(10)
      background = new Color(58, 129, 159)
    }

    listenTo(startHouseButton)
    listenTo(stopHouseButton)
    listenTo(tablesTable.selection)

    reactions += {
      case ButtonClicked(`startHouseButton`) =>
        clickStartHouse()
      case ButtonClicked(`stopHouseButton`) =>
        clickStopHouse()
    }
    debug("starting the House")
    House.start
  }

  /** Start House method will send the Go message to House */
  def clickStartHouse(): Unit = {
    println("Clicked startHouse button")
    bjFrame.startHouseButton.enabled = false
    bjFrame.stopHouseButton.enabled = true
    House ! Go
    bjFrame.tablesTable.visible = true
  }

  /** Stop House method will send the Stop message to House and Exit */
  def clickStopHouse(): Unit = {
    println("Clicked stopHouse button")
    House ! Stop
    System.exit(0)
    bjFrame.tablesTable.visible = false
    bjFrame.stopHouseButton.enabled = false
    bjFrame.startHouseButton.enabled = true
  }
}